#!/bin/sh
# postinst script for openwga
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    configure)        
    mv /var/lib/openwga/server.conf /etc/openwga
    
    invoke-rc.d openwga stop
    
	addgroup --system openwga
	adduser --system --home /var/lib/openwga --no-create-home --ingroup openwga --disabled-login openwga

	chown -R openwga:openwga /var/lib/openwga
	chown -R openwga:openwga /etc/openwga

	update-rc.d -f openwga defaults

	chmod 0600 /etc/openwga/jmx.access
	chmod 0600 /etc/openwga/jmx.passwd

	TOMCAT_LOGS=/var/lib/openwga/tomcat/logs
	if [ ! -e $TOMCAT_LOGS ] ; then
		mkdir $TOMCAT_LOGS
		chown -R openwga:openwga $TOMCAT_LOGS
	fi
	
	TOMCAT_WORK="/var/lib/openwga/tomcat/work/Catalina/localhost/_"
	ROOT_DEPLOYMENT="/var/lib/openwga/tomcat/webapps/ROOT"
	if [ -e $TOMCAT_WORK ] ; then	
		rm -r $TOMCAT_WORK
	fi
    if [ -e $ROOT_DEPLOYMENT ] ; then
		rm -r $ROOT_DEPLOYMENT
	fi	
    
    TOMCAT_CONF="/var/lib/openwga/tomcat/conf"
    if [ ! -h $TOMCAT_CONF ] ; then
    	ln -s /etc/openwga/tomcat/conf $TOMCAT_CONF
    fi

	OPENWGA_CONF="/var/lib/openwga/wgaconfig.xml"
	if [ ! -h $OPENWGA_CONF ] ; then
    	ln -s /etc/openwga/wgaconfig.xml $OPENWGA_CONF
    fi

	invoke-rc.d openwga start
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0


